package com.beyond.pm.ui.home.graph

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MenuOpen
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.beyond.pm.bean.RouteScreenParameter
import com.beyond.pm.component.MyIconButton
import com.beyond.pm.component.MyTopBar
import com.beyond.pm.component.MyTopBarStyle
import com.beyond.pm.component.chart.BarChart
import com.beyond.pm.component.chart.HorizontalBarChart
import com.beyond.pm.component.chart.LineChart
import com.beyond.pm.component.chart.PieChart
import com.beyond.pm.component.chart.RadarChart
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.data.RadarEntry

/**
 * @author chen
 * @time 2023/5/31 13:34
 * @name ProjectManage
 * @class describe
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GraphScreen(enter: Boolean, openDrawer: () -> Unit) {
    //    val scrollState = rememberScrollState() //  Column 保存状态的方法
//    val scrollState = rememberLazyListState() // LazyColumn 用这个保存位置状态

// https://zhuanlan.zhihu.com/p/22134046
    Scaffold(
        topBar = {
            MyTopBar(
                style = MyTopBarStyle.CenterAligned,
                navigationIcon = {
                    MyIconButton(
                        imageVector = Icons.Default.MenuOpen,
                        onClick = openDrawer
                    )
                },
                title = {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            imageVector = RouteScreenParameter.Graph.bean.icon,
                            contentDescription = null,
                        )
                        Spacer(modifier = Modifier.width(10.dp))
                        Text(text = stringResource(id = RouteScreenParameter.Graph.bean.labelId))
                    }
                },
            )
        },
    ) {
        if (enter)
            LazyColumn(
                modifier = Modifier
                    .padding(
                        PaddingValues(
                            start = 0.dp,
                            top = it.calculateTopPadding(),
                            end = 0.dp,
                            bottom = 0.dp
                        )
                    )
                    .fillMaxSize(),
                horizontalAlignment = Alignment.Start
            ) {
                item {
                    Text("用户统计", modifier = Modifier.padding(bottom = 5.dp))
                    MyLineChart()
                    BarChartPreview()
                    PieChartPreview()
                    RadarChartPreview()
                    HorizontalBarChartPreview()
                }
            }
    }
}

@Composable
private fun MyLineChart() {
    val entries = mutableListOf(
        Entry(1f, 15f),
        Entry(2f, 65f),
        Entry(4f, 98f),
        Entry(5f, 82f),
        Entry(6f, 48f),
        Entry(12f, 65f),
        Entry(14f, 98f),
        Entry(15f, 22f),
        Entry(16f, 48f),
        Entry(20f, 20f),
        Entry(21f, 15f),
        Entry(22f, 65f),
        Entry(23f, 30f),
        Entry(24f, 98f),
        Entry(25f, 212f),
        Entry(26f, 48f),
        Entry(30f, 20f),
        Entry(31f, 45f),
    )
    LineChart(
        modifier = Modifier
            .height(200.dp)
            .padding(horizontal = 10.dp, vertical = 10.dp),
        entries = entries,
        dataSetLabel = "数据集"
    )
}

@Composable
private fun BarChartPreview() {
    val entries = mutableListOf(
        BarEntry(0f, 20f),
        BarEntry(1f, 15f),
        BarEntry(2f, 25f),
        BarEntry(3f, 30f),
        BarEntry(4f, 18f),
        BarEntry(5f, 22f),
        BarEntry(6f, 28f),
    )

    BarChart(
        modifier = Modifier
            .height(200.dp)
            .padding(horizontal = 10.dp, vertical = 10.dp),
        entries = entries,
        dataSetLabel = "数据集"
    )
}

@Composable
private fun HorizontalBarChartPreview() {
    val entries = mutableListOf(
        BarEntry(0f, 20f),
        BarEntry(1f, 15f),
        BarEntry(2f, 25f),
        BarEntry(3f, 30f),
        BarEntry(4f, 18f),
        BarEntry(5f, 22f),
        BarEntry(6f, 28f),
        BarEntry(7f, 48f),
    )

    HorizontalBarChart(
        modifier = Modifier
            .height(200.dp)
            .padding(horizontal = 10.dp, vertical = 10.dp),
        entries = entries,
        dataSetLabel = "数据集"
    )
}

@Composable
private fun PieChartPreview() {
    val entries = mutableListOf(
        PieEntry(20f, "第一项"),
        PieEntry(15f, "第二项"),
        PieEntry(25f, "第三项"),
        PieEntry(30f, "第四项")
    )

    PieChart(
        modifier = Modifier
            .height(250.dp),
        entries = entries,
        dataSetLabel = "数据集"
    )
}

@Composable
private fun RadarChartPreview() {
    val entries = mutableListOf(
        RadarEntry(5f),
        RadarEntry(4f),
        RadarEntry(6f),
        RadarEntry(8f),
        RadarEntry(7f),
    )

    RadarChart(
        modifier = Modifier
            .height(300.dp),
        entries = entries,
        dataSetLabel = "数据集"
    )
}
